# -*- coding: utf-8 -*-
# Zope imports
import zope.component
from random import randint
from zope.interface import implements
from Acquisition import aq_inner
from zope.component import getMultiAdapter

# Plone, Five imports
from Products.CMFPlone.utils import getToolByName, base_hasattr
from Products.Five import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.CMFCore.utils import getToolByName
# Product imports
# import pdb; pdb.set_trace()

class EntidadD(BrowserView):

    template = ViewPageTemplateFile('templates/entidadD.pt')

    def __call__(self):
        if 'bienvenidos' not in self.context.objectIds():
            self.context.invokeFactory("Document", "bienvenidos", title='Bienvenidos')

        if 'galeriasLista' not in self.context.objectIds():
            self.context.invokeFactory("Topic", "galeriasLista", title='Galerias de la entidad')
#            import pdb
#            pdb.set_trace()
            self.context.galeriasLista.setLayout('atct_topic_view_entidad')
            type_crit = self.context.galeriasLista.addCriterion('Type','ATPortalTypeCriterion')
            type_crit.setValue(['Gallery'])
            sort_crit = self.context.galeriasLista.addCriterion('effective','ATSortCriterion')
            self.context.galeriasLista.getSortCriterion()
            path_crit = self.context.galeriasLista.addCriterion('path','ATPathCriterion')
            path_crit.setValue(self.context.UID())
            path_crit.setRecurse(True)
            print 'adding path criteria'

        if 'noticiasLista' not in self.context.objectIds():
            self.context.invokeFactory("Topic", "noticiasLista", title='Noticias de la entidad')
 #           self.context.noticiasLista.setLayout('atct_topic_view_entidad')
            type_crit = self.context.noticiasLista.addCriterion('Type','ATPortalTypeCriterion')
            type_crit.setValue(['News Item'])
            self.context.noticiasLista.getSortCriterion()
            path_crit = self.context.noticiasLista.addCriterion('path','ATPathCriterion')
            path_crit.setValue(self.context.UID())
            path_crit.setRecurse(True)
            sort_crit = self.context.noticiasLista.addCriterion('effective','ATSortCriterion')

        if 'servicios' not in self.context.objectIds():
            self.context.invokeFactory("Document", "servicios", title='Servicios')

        if 'contactenos' not in self.context.objectIds():
            self.context.invokeFactory("Document", "contactenos", title='Contactenos')
        return self.template()

    def __init__(self, context, request):
        self.context = context
        self.request = request
        #self.createObjectsStartup(context)
#        self.invokeFactory("Document", "Bienvenidoss")

    def isBanner(self):
        items = self.context.contentItems()
        if 'banner' in items:
            return 1
        else:
            return None

